package com.lanswon.qzsmk.controller.systemManageController;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.controller.cardManageController.BaseController;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.model.Org;
import com.lanswon.qzsmk.service.systemManageService.BranchManageService;
import com.lanswon.qzsmk.service.systemManageService.TellerManageService;
import com.lanswon.qzsmk.vo.BranchVo;
import com.lanswon.qzsmk.vo.SaveOrUpdateBranchVo;
import com.lanswon.qzsmk.vo.UserVo;


/**
 * 网点管理Controller
 * @author guyawei
 *
 */
@Controller
@RequestMapping(value="/branchManage")
public class BranchManageController  {

	@Autowired
	BranchManageService bms;
	@Autowired
	TellerManageService tms;
	@Autowired
	//OrgMapper om;
	
	/**
	 * 链接网点管理页面
	 * @return
	 */
	@RequiresPermissions("branchManage:index")
	@RequestMapping(value="/branchManageIndex",method={RequestMethod.GET,RequestMethod.POST},name="网点管理,branchManage:index")
	public String tellerManage(){
		
		return "BranchManage";
	}
	
	/**
	 * 链接增加网点页面
	 * @return
	 */
	@RequiresPermissions("branchManage:add")
	@RequestMapping(value="/addBranch",method=RequestMethod.GET,name="网点管理增加网点页面,branchManage:add")
	public String addTeller(HttpServletRequest request){
		request.getSession().setAttribute("sign", true);
		return "AddBranch";
	}
	
	/**
	 * 链接修改网点页面
	 * @param parm
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("branchManage:modify")
	@RequestMapping(value="/goUpdateBranch",method=RequestMethod.GET,name="网点管理修改网点页面,branchManage:modify")
	public String goUpdateTeller(String parm,HttpServletRequest request) throws Exception{
		request.getSession().setAttribute("sign", false);
		SaveOrUpdateBranchVo branch=bms.queryBranchByNo(Integer.valueOf(parm));
		request.getSession().setAttribute("branch", branch);
		return "AddBranch";
	}
	/**
	 * 网点分配柜员
	 * @param parm
	 * @param request
	 * @return
	 * @throws Exception 
	 * @throws NumberFormatException 
	 */
	@RequiresPermissions("branchManage:allotTeller")
	@RequestMapping(value="/allotTeller",method=RequestMethod.GET,name="网点管理网点分配柜员,branchManage:allotTeller")
	public String allotTeller(String parm,HttpServletRequest request) throws NumberFormatException, Exception{
		Map<String, Object> map=new HashMap<>();
		Org org=bms.queryOrgByNo(Integer.valueOf(parm));
		map.put("info", org);
		System.out.println(parm);
		request.getSession().setAttribute("map",map );
		request.getSession().setAttribute("parm",parm );
		return "AllotTeller";
	}
	
	/**
	 *网点分配柜员 获取网点信息
	 * @param request
	 * @return
	 */
	@RequiresPermissions("branchManage:getInfoByAllotTeller")
	@RequestMapping(value="getInfoByAllotTeller",method=RequestMethod.POST,name="网点管理网点分配柜员获取网点信息,branchManage:getInfoByAllotTeller")
	@ResponseBody
	public Map<String, Object> getInfoByAllotTeller(HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String, Object> map=(Map<String, Object>) request.getSession().getAttribute("map");
		request.getSession().removeAttribute("map");
		return map;
	}
	
	/**
	 * 网点分配柜员获取柜员信息
	 * @param request
	 * @param uv
	 * @param pageCount
	 * @return
	 */
	@RequiresPermissions("branchManage:getTellerInfoByAllot")
	@ResponseBody
	@RequestMapping(value="/getTellerInfoByAllot",method=RequestMethod.POST,name="网点管理网点分配柜员获取柜员信息,branchManage:getTellerInfoByAllot")
	public DwzPage<UserVo> getTellerInfoByAllot(HttpServletRequest request,UserVo uv,Integer numPerPage,Integer pageNum){
		String branchNo=(String) request.getSession().getAttribute("parm");
		request.getSession().removeAttribute("parm");
		if(branchNo!=null){
			uv.setBranchNo(Integer.valueOf(branchNo));
		}
//		if(uv.getNumPerPage()==null){//加载页面时执行
//			uv.setCurrentPage(1);
//			uv.setNumPerPage(20);
//		}else {
//			if(uv.getCurrentPage()<=1||pageCount==0){
//				uv.setCurrentPage(1);
//			}else if(uv.getCurrentPage()>=pageCount&&pageCount!=0){
//				uv.setCurrentPage(pageCount);
//			}
//		}
		System.out.println(pageNum+","+numPerPage);
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;			
		}
		uv.setCurrentPage(pageNum);
		uv.setNumPerPage(numPerPage);
		uv.setStartLine((uv.getCurrentPage()-1)*uv.getNumPerPage());
		DwzPage<UserVo> dPage=tms.showTellerByBranch(uv);
		;
		return dPage;
		
	}
	
	/**
	 * 分配柜员提交
	 * @param branchNo
	 * @param allotTeller
	 * @return
	 * @throws NumberFormatException
	 * @throws Exception
	 */
	@RequiresPermissions("branchManage:commitAllotTeller")
	@RequestMapping(name="网点管理分配柜员提交,branchManage:commitAllotTeller",value="/commitAllotTeller",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commitAllotTeller(String branchNo,String userNo) throws NumberFormatException, Exception{
		System.out.println("branchNo="+branchNo+","+"userNo="+userNo);
		if(!branchNo.equals("null")){
			System.out.println("++++++++++++++++++++++++++");
			tms.allocatTeller(Integer.valueOf(userNo),Integer.valueOf(branchNo));
			
		}else{
			tms.allocatTeller(Integer.valueOf(userNo),null);
		}
		
		Map<String, Object> map=new HashMap<>();
		map.put("statusCode", "200");
		//map.put("navTabId", "branchManage");
		//map.put("callbackType", "closeCurrent");
		//map.put("forwardUrl", "branchManage/branchManageIndex");//post请求
		return map;
		
		
	}
	/**
	 * 获取网点信息
	 * @param bv
	 * @param pageCount
	 * @return
	 */
	@RequiresPermissions("branchManage:getBranchInfo")
	@RequestMapping(name="网点管理获取网点信息,branchManage:getBranchInfo",value="/getBranchInfo",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<Org> getBranchInfo(BranchVo bv,Integer numPerPage,Integer pageNum){
//		if(bv.getNumPerPage()==null){//加载页面时执行
//			bv.setCurrentPage(1);
//			bv.setNumPerPage(20);
//		}else {
//			if(bv.getCurrentPage()<=1||pageCount==0){
//				bv.setCurrentPage(1);
//			}else if(bv.getCurrentPage()>=pageCount&&pageCount!=0){
//				bv.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;			
		}
		bv.setCurrentPage(pageNum);
		bv.setNumPerPage(numPerPage);
		bv.setStartLine((bv.getCurrentPage()-1)*bv.getNumPerPage());
		DwzPage<Org> dPage=bms.queryBranchByName(bv);
		return dPage;
	}
	
	/**
	 * 获取要修改的信息
	 * @param request
	 * @return
	 */
	@RequiresPermissions("branchManage:getUpdateBranchInfo")
	@ResponseBody
	@RequestMapping(name="网点管理获取要修改的信息,branchManage:getUpdateBranchInfo",value="/getUpdateBranchInfo",method=RequestMethod.POST)
	public Map<String, Object> getUpdateInfo(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		map.put("sign",request.getSession().getAttribute("sign"));
		map.put("branch",request.getSession().getAttribute("branch"));
		request.getSession().removeAttribute("sign");
		request.getSession().removeAttribute("branch");
		return map;	
	}
	
	/**
	 * 修改或者保存
	 * @param branchVO
	 * @param applyTime
	 * @param activeTime
	 * @param laeTime
	 * @return
	 * @throws ParseException
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("branchManage:updateBranch")
	@RequestMapping(name="网点管理修改或者保存,branchManage:updateBranch",value="/updateBranch",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateBranch(SaveOrUpdateBranchVo branchVO ,String timePicker1,String timePicker2) throws ParseException, ApplicationException{
//		if(activeTime!=null&&activeTime!=""){
//			branchVO.setActiveDate(TellerManageController.dateFormat(activeTime));
//		}
//		if(applyTime!=null&&applyTime!=""){
//			branchVO.setApplyDate(TellerManageController.dateFormat(applyTime));
//		}
//		if(laeTime!=null&&laeTime!=""){
//			branchVO.setLaeDate(TellerManageController.dateFormat(laeTime));
//		}
		if(branchVO.getOrgNo()==null){
			//新增
			branchVO.setServerDate(timePicker1+"-"+timePicker2);
			bms.addBranch(branchVO);
		}else{
			//修改
			branchVO.setServerDate(timePicker1+"-"+timePicker2);
			bms.modifyBranch(branchVO);
		}
		Map<String, Object> map=new HashMap<>();
		map.put("statusCode", "200");
		map.put("message", "操作成功");
		map.put("navTabId", "branchManage");
		map.put("callbackType", "closeCurrent");
		map.put("forwardUrl", "branchManage/branchManageIndex");//post请求
		return map;
	}
	
}
